﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.DAL;

namespace TravelLink.WebApp.Admin
{
    public partial class AdminLogin : System.Web.UI.Page, ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Session.Contents.RemoveAll();
                Session.RemoveAll();
                Request.Cookies.Clear();
                Response.Cookies.Clear();
            }
        }

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    if (this.tbusername.Text.Trim() != "admin" || this.tbpassword.Text.Trim() != "admin")
        //    {
        //        Response.Write(string.Format("<script>alert('{0}')</script>", "用户名或密码错误"));
        //    }
        //    else
        //    {
        //        Session["AdminUser"] = "admin";
        //        Response.Redirect("AdminDefault.aspx");
        //    }
        //}

        private string CallBackValue = string.Empty;

        #region ICallbackEventHandler 成员

        public string GetCallbackResult()
        {
            return CallBackValue;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            try
            {
                string[] eArg = eventArgument.Split(',');
                string usercode = eArg[0];
                string password = eArg[1];
                //
                if (usercode == "ADMINISTRATOR")
                {
                    if (System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower() != System.Configuration.ConfigurationManager.AppSettings["superpwd"])
                    {
                        this.CallBackValue = "密码错误";
                        return;
                    }
                    Session["Admin"] = new TL_Admin() { UserCode = "ADMINISTRATOR" };
                    Response.Cookies["user"].Value = "ADMINISTRATOR";
                    Response.Cookies["user"].Expires = DateTime.MaxValue; 
                    CallBackValue = "";
                    return;
                }
                //
                var dc = DAL.DataContextFactory.Create();
                var user= dc.TL_Admin.Where(t => t.UserCode == usercode).FirstOrDefault();
                if (user == null)
                {
                    this.CallBackValue = "无此用户";
                    return;
                }
                if (!(user.Enable??false))
                {
                    this.CallBackValue = "此用户无法登录";
                    return;
                }
                if (user.Password != System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5").ToLower())
                {
                    this.CallBackValue = "密码错误";
                    return;
                }
                Session["Admin"] = user;
                Response.Cookies["user"].Value = user.UserCode;
                Response.Cookies["user"].Expires = DateTime.MaxValue; 
                CallBackValue = "";
            }
            catch (Exception ex)
            {
                this.CallBackValue = ex.Message;
            }
        }

        #endregion
    }
}